package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.Dto.SmsFlashPromotionSessionDto;
import com.itheima.common.domain.AjaxResult;
import com.itheima.service.ISmsFlashPromotionProductRelationService;
import com.itheima.service.ISmsFlashPromotionSessionService;
import com.itheima.sms.entity.SmsFlashPromotionProductRelation;
import com.itheima.sms.entity.SmsFlashPromotionSession;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/flashSession")
public class SmsFlashPromotionSessionController {

    @Autowired
    private ISmsFlashPromotionSessionService smsFlashPromotionSessionService;

    @Autowired
    private ISmsFlashPromotionProductRelationService smsFlashPromotionProductRelationService;

    /**
     * 添加场次
     *
     * @param smsFlashPromotionSession
     * @return
     */
    @PostMapping("/create")
    public AjaxResult create(@RequestBody SmsFlashPromotionSession smsFlashPromotionSession) {
        smsFlashPromotionSessionService.save(smsFlashPromotionSession);
        return AjaxResult.success();
    }

    /**
     * 删除场次
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        smsFlashPromotionSessionService.removeById(id);
        return AjaxResult.success();
    }

    /**
     * 获取全部场次
     *
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list() {
        List<SmsFlashPromotionSession> list = smsFlashPromotionSessionService.list();
        return AjaxResult.success(list);
    }

    /**
     * 获取全部可选场次及其数量
     *
     * @param flashPromotionId
     * @return
     */
    @GetMapping("/selectList")
    public AjaxResult selectList(@RequestParam(value = "flashPromotionId", required = false) Integer flashPromotionId) {
        //查找出数量
        QueryWrapper<SmsFlashPromotionProductRelation> qw = new QueryWrapper<>();
        qw.select("flash_promotion_session_id,count(*) as productCount")
                .eq("flash_promotion_id", flashPromotionId)
                .groupBy("flash_promotion_session_id");

        List<Map<String, Object>> maps = smsFlashPromotionProductRelationService.listMaps(qw);
        ArrayList<SmsFlashPromotionSessionDto> list = new ArrayList<>();


        //将数量装到dto里
        for (Map<String, Object> map : maps) {
            SmsFlashPromotionSessionDto smsFlashPromotionSessionDto = new SmsFlashPromotionSessionDto();

            LambdaQueryWrapper<SmsFlashPromotionSession> lqw = new LambdaQueryWrapper<>();
            lqw.eq(SmsFlashPromotionSession::getId, map.get("flash_promotion_session_id"));
            SmsFlashPromotionSession smsFlashPromotionSession = smsFlashPromotionSessionService.getOne(lqw);
            BeanUtils.copyProperties(smsFlashPromotionSession, smsFlashPromotionSessionDto);
            smsFlashPromotionSessionDto.setProductCount((Long) map.get("productCount"));

            list.add(smsFlashPromotionSessionDto);
        }
        return AjaxResult.success(list);
    }

    /**
     * 修改启用状态
     *
     * @param id
     * @param map
     * @return
     */
    @PostMapping("/update/status/{id}")
    public AjaxResult updateStatus(@PathVariable Long id,@RequestParam("status") Integer status) {
        LambdaUpdateWrapper<SmsFlashPromotionSession> luw = new LambdaUpdateWrapper();
       luw.eq(SmsFlashPromotionSession::getId, id)
               .set(SmsFlashPromotionSession::getStatus,status);
        smsFlashPromotionSessionService.update(luw);

        return AjaxResult.success();
    }

    /**
     * 修改场次
     * @param id
     * @param smsFlashPromotionSession
     * @return
     */
    @PostMapping("/update/{id}")
    public AjaxResult update(@PathVariable Long id, @RequestBody SmsFlashPromotionSession smsFlashPromotionSession) {
        LambdaUpdateWrapper<SmsFlashPromotionSession> luw = new LambdaUpdateWrapper();
        luw.eq(SmsFlashPromotionSession::getId, id)
                .set(SmsFlashPromotionSession::getCreateTime, smsFlashPromotionSession.getCreateTime())
                .set(SmsFlashPromotionSession::getEndTime, smsFlashPromotionSession.getEndTime())
                .set(SmsFlashPromotionSession::getId, smsFlashPromotionSession.getId())
                .set(SmsFlashPromotionSession::getName, smsFlashPromotionSession.getName())
                .set(SmsFlashPromotionSession::getStartTime, smsFlashPromotionSession.getStartTime())
                .set(SmsFlashPromotionSession::getStatus, smsFlashPromotionSession.getStatus());
        smsFlashPromotionSessionService.update(luw);
        return AjaxResult.success();
    }

    /**
     * 获取场次详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id){
        SmsFlashPromotionSession byId = smsFlashPromotionSessionService.getById(id);
        return AjaxResult.success(byId);
    }

}
